﻿Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports GMap.NET.WindowsForms.ToolTips
Imports GMap.NET

Imports System
Imports System.Net
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System.Data
Imports MySql.Data.MySqlClient
Imports System.IO
Imports GMap.NET.CacheProviders
Imports DevExpress.XtraGrid.Views.Grid




Public Class OpenMap

    Private dataAdapter As New MySqlDataAdapter

    Dim Time As Integer = 30

    Dim screen As GMapOverlay
    Dim objects As GMapOverlay
    Dim routes As GMapOverlay
    Dim car_layer As GMapOverlay

    Dim SV_200 As GMapOverlay
    Dim SV_400 As GMapOverlay
    Dim SV_500 As GMapOverlay
    Dim SV_600 As GMapOverlay
    Dim SV_610 As GMapOverlay
    Dim SV_700 As GMapOverlay

    Dim WM_200 As GMapOverlay
    Dim WM_400 As GMapOverlay
    Dim WM_500 As GMapOverlay
    Dim WM_600 As GMapOverlay
    Dim WM_610 As GMapOverlay
    Dim WM_700 As GMapOverlay

    Dim NO_200 As GMapOverlay
    Dim NO_400 As GMapOverlay
    Dim NO_500 As GMapOverlay
    Dim NO_600 As GMapOverlay
    Dim NO_610 As GMapOverlay
    Dim NO_700 As GMapOverlay

    Dim History As GMapOverlay

    Dim r_start As PointLatLng
    Dim r_end As PointLatLng
    Dim currentMarker As CarMarker

    Public ActivTech As String = ""

    Dim CurentRectMarker As GMapMarkerCross = Nothing
    Dim center As GMapMarker
    Dim isMouseDown As Boolean = False

    Private Sub OpenMap_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        My.Settings.OpenMapSize = Me.Size
        My.Settings.OpenMapPos = Me.Location

        'Save all gridview setups
        If Not Directory.Exists(My.Settings.ConfigPath) Then Directory.CreateDirectory(My.Settings.ConfigPath)

        Try
            GridView1.SaveLayoutToXml(My.Settings.ConfigPath & "\OpenMapGW01.xml")
            GridView2.SaveLayoutToXml(My.Settings.ConfigPath & "\OpenMapGW02.xml")
        Catch ex As Exception
        End Try

    End Sub

    Private Sub OpenMap_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CADISDataSetTech.F0010' table. You can move, or remove it, as needed.
        Me.F0010TableAdapter.Fill(Me.CADISDataSetTech.F0010)

        Me.Size = My.Settings.OpenMapSize
        Me.Location = My.Settings.OpenMapPos

        If My.Settings.TechTrace Then RibbonPageGroup6.Visible = True
        BarEditItem1.EditValue = Date.Today

        If My.Settings.Address Then
            RibbonPage3.Visible = True
        Else
            RibbonPage3.Visible = False
        End If

        Me.SplitContainerControl1.SplitterPosition = My.Settings.Split2
        BarEditItem2.EditValue = Date.Today
        BarEditItem3.EditValue = Date.Today



        'Local Map cache
        Dim ch As MsSQLPureImageCache = New MsSQLPureImageCache
        ch.ConnectionString = My.Settings.PFleet

        'Config Gmap
        GMaps.Instance.ImageCacheSecond = ch
        'GMaps.Instance.Language = "de-ch"
        GMaps.Instance.Mode = AccessMode.ServerAndCache

        '                    GMaps.Instance.UseRouteCache = true;
        '           GMaps.Instance.UseGeocoderCache = true;
        '          GMaps.Instance.UsePlacemarkCache = true;


        Dim proxy As String = My.Settings.Proxy
        Dim proxyport As Integer = Val(My.Settings.ProxyPort)

        'Setup the proxy only if evalable
        If My.Computer.Network.Ping(My.Settings.Proxy) Then
            Me.GMapControl1.Manager.Proxy = New WebProxy(proxy, proxyport)
        End If

        Me.GMapControl1.MapType = GMap.NET.MapType.BingMap

        Me.GMapControl1.MinZoom = Val(My.Settings.MinZoom)
        Me.GMapControl1.MaxZoom = Val(My.Settings.MaxZoom)
        Me.GMapControl1.Zoom = Val(My.Settings.Zoom)

        'adjust zoom bar
        Me.TrackBarControl1.Properties.Minimum = Val(My.Settings.MinZoom)
        Me.TrackBarControl1.Properties.Maximum = Val(My.Settings.MaxZoom)
        Me.TrackBarControl1.Value = Val(My.Settings.Zoom)

        ' map center
        Me.GMapControl1.SetCurrentPositionByKeywords(My.Settings.PFleetCity)

        screen = New GMapOverlay(GMapControl1, "ScreenCenter")
        GMapControl1.Overlays.Add(screen)

        routes = New GMapOverlay(GMapControl1, "routes")
        GMapControl1.Overlays.Add(routes)

        car_layer = New GMapOverlay(GMapControl1, "car")
        GMapControl1.Overlays.Add(car_layer)

        SV_200 = New GMapOverlay(GMapControl1, "SV_200")
        GMapControl1.Overlays.Add(SV_200)
        SV_400 = New GMapOverlay(GMapControl1, "SV_400")
        GMapControl1.Overlays.Add(SV_400)
        SV_500 = New GMapOverlay(GMapControl1, "SV_500")
        GMapControl1.Overlays.Add(SV_500)
        SV_600 = New GMapOverlay(GMapControl1, "SV_600")
        GMapControl1.Overlays.Add(SV_600)
        SV_610 = New GMapOverlay(GMapControl1, "SV_610")
        GMapControl1.Overlays.Add(SV_610)
        SV_700 = New GMapOverlay(GMapControl1, "SV_700")
        GMapControl1.Overlays.Add(SV_700)

        WM_200 = New GMapOverlay(GMapControl1, "WM_200")
        GMapControl1.Overlays.Add(WM_200)
        WM_400 = New GMapOverlay(GMapControl1, "WM_400")
        GMapControl1.Overlays.Add(WM_400)
        WM_500 = New GMapOverlay(GMapControl1, "WM_500")
        GMapControl1.Overlays.Add(WM_500)
        WM_600 = New GMapOverlay(GMapControl1, "WM_600")
        GMapControl1.Overlays.Add(WM_600)
        WM_610 = New GMapOverlay(GMapControl1, "WM_610")
        GMapControl1.Overlays.Add(WM_610)
        WM_700 = New GMapOverlay(GMapControl1, "WM_700")
        GMapControl1.Overlays.Add(WM_700)

        NO_200 = New GMapOverlay(GMapControl1, "NO_200")
        GMapControl1.Overlays.Add(NO_200)
        NO_400 = New GMapOverlay(GMapControl1, "NO_400")
        GMapControl1.Overlays.Add(NO_400)
        NO_500 = New GMapOverlay(GMapControl1, "NO_500")
        GMapControl1.Overlays.Add(NO_500)
        NO_600 = New GMapOverlay(GMapControl1, "NO_600")
        GMapControl1.Overlays.Add(NO_600)
        NO_610 = New GMapOverlay(GMapControl1, "NO_610")
        GMapControl1.Overlays.Add(NO_610)
        NO_700 = New GMapOverlay(GMapControl1, "NO_700")
        GMapControl1.Overlays.Add(NO_700)

        History = New GMapOverlay(GMapControl1, "History")
        GMapControl1.Overlays.Add(History)



        center = New GMapMarkerCross(GMapControl1.Position)
        screen.Markers.Add(center)

        GetData()

        SetLayout()

        If File.Exists(My.Settings.ConfigPath & "\OpenMapGW01.xml") Then GridView1.RestoreLayoutFromXml(My.Settings.ConfigPath & "\OpenMapGW01.xml")

    End Sub

    Function SetLayout()

        BC_SV_200.Checked = My.Settings.BC_SV_200
        BC_SV_400.Checked = My.Settings.BC_SV_400
        BC_SV_500.Checked = My.Settings.BC_SV_500
        BC_SV_600.Checked = My.Settings.BC_SV_600
        BC_SV_610.Checked = My.Settings.BC_SV_610
        BC_SV_700.Checked = My.Settings.BC_SV_700

        BC_WM_200.Checked = My.Settings.BC_WM_200
        BC_WM_400.Checked = My.Settings.BC_WM_400
        BC_WM_500.Checked = My.Settings.BC_WM_500
        BC_WM_600.Checked = My.Settings.BC_WM_600
        BC_WM_610.Checked = My.Settings.BC_WM_610
        BC_WM_700.Checked = My.Settings.BC_WM_700

        BC_NO_200.Checked = My.Settings.BC_NO_200
        BC_NO_400.Checked = My.Settings.BC_NO_400
        BC_NO_500.Checked = My.Settings.BC_NO_500
        BC_NO_600.Checked = My.Settings.BC_NO_600
        BC_NO_610.Checked = My.Settings.BC_NO_610
        BC_NO_700.Checked = My.Settings.BC_NO_700


        SV_200.IsVisibile = BC_SV_200.Checked
        SV_400.IsVisibile = BC_SV_400.Checked
        SV_500.IsVisibile = BC_SV_500.Checked
        SV_600.IsVisibile = BC_SV_600.Checked
        SV_610.IsVisibile = BC_SV_610.Checked
        SV_700.IsVisibile = BC_SV_700.Checked

        WM_200.IsVisibile = BC_WM_200.Checked
        WM_400.IsVisibile = BC_WM_400.Checked
        WM_500.IsVisibile = BC_WM_500.Checked
        WM_600.IsVisibile = BC_WM_600.Checked
        WM_610.IsVisibile = BC_WM_610.Checked
        WM_700.IsVisibile = BC_WM_700.Checked

        NO_200.IsVisibile = BC_NO_200.Checked
        NO_400.IsVisibile = BC_NO_400.Checked
        NO_500.IsVisibile = BC_NO_500.Checked
        NO_600.IsVisibile = BC_NO_600.Checked
        NO_610.IsVisibile = BC_NO_610.Checked
        NO_700.IsVisibile = BC_NO_700.Checked

        SetLayout = True

    End Function

    Function GetData()

        Try
            Dim ConCM As MySqlConnection
            Dim Reader As MySqlDataReader = Nothing
            Dim x As Decimal
            Dim y As Decimal
            Dim tech As String
            Dim tech1 As String
            Dim i As Integer = 0
            Dim ii As Integer = 0
            Dim pos As PointLatLng
            Dim posnew As PointLatLng
            Dim move As Boolean = False
            Dim impBMW As System.Drawing.Image = My.Resources.Resources.HWcar_2_icon_32
            Dim impVW As System.Drawing.Image = My.Resources.Resources.HWcar_5_icon_32
            Dim TechID As String

            Dim DeltaPos As Boolean = False

            routes.Routes.Clear()

            ii = car_layer.Markers.Count - 1

            ConCM = New MySqlConnection(My.Settings.MySQL)
            Dim cmd As New MySqlCommand("select  CAST(ValGeoDatLatitudea10a / 3600000 AS Decimal (18,13)), CAST(ValGeoDatLongitudefeb0 / 3600000 AS Decimal(18,13)), ValAdrName1fe8, ValTechnikerid from technikerrow where  ValGeoDatLatitudea10a <> '' and ValAdrLaendercodec9f0 = '" & My.Settings.CadisEnv & "' and ValDieStatus5a94 = '1' ", ConCM)
            ConCM.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read

                move = False

                x = Reader(0).ToString
                y = Reader(1).ToString
                tech = Trim(Reader(2).ToString)
                TechID = Reader(3).ToString
                posnew = New PointLatLng(x, y)

                ii = car_layer.Markers.Count - 1

                For i = 1 To ii
                    If car_layer.Markers(ii).Tag = "AUTO" Then
                        Dim p1, p2 As Decimal

                        Dim Tempmarker As CarMarker = car_layer.Markers(i)

                        p1 = Tempmarker.Position.Lat
                        p2 = Tempmarker.Position.Lng
                        pos = New PointLatLng(p1, p2)
                        tech1 = Tempmarker.ToolTipText.ToString

                        DeltaPos = False

                        If p1 - x > 0.001 Or p1 - x < -0.001 Then
                            DeltaPos = True
                        End If
                        If p2 - y > 0.001 Or p2 - y < -0.001 Then
                            DeltaPos = True
                        End If


                        If (TechID & " / " & tech) = tech1 Then
                            move = True
                            Tempmarker.Position = posnew

                            If DeltaPos Then
                                Tempmarker.Image = impVW
                            Else
                                Tempmarker.Image = impBMW
                            End If
                        End If

                        'car_layer.Markers.Remove(Tempmarker)
                        'ii = car_layer.Markers.Count - 1
                    End If
                Next

                If Not move Then
                    currentMarker = New CarMarker(New PointLatLng(x, y))

                    ' Dim b() As Byte = System.Text.Encoding.UTF8.GetBytes(tech)
                    ' Dim s1 As String = System.Text.Encoding.Default.GetString(b)

                    currentMarker.ToolTipText = TechID & " / " & tech
                    currentMarker.IsHitTestVisible = True
                    currentMarker.Tag = "AUTO"
                    currentMarker.Image = impBMW
                    car_layer.Markers.Add(currentMarker)
                End If
            End While

            ConCM.Close()

        Catch ex As OleDbException

        End Try
        GetData = ""

    End Function

    Function GetData1(ByVal selectCommand As String)

        Try
            Dim ConCM As MySqlConnection
            ConCM = New MySqlConnection(My.Settings.MySQL)
            ConCM.Open()

            Dim cmd As New MySqlCommand(selectCommand, ConCM)

            Me.dataAdapter = New MySqlDataAdapter(cmd)
            Dim table As New DataTable
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.BindingSource1.DataSource = table

            ' Resize the DataGridView columns to fit the newly loaded content.
            'Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
            GridView2.BestFitColumns()
            If File.Exists(My.Settings.ConfigPath & "\OpenMapGW02.xml") Then GridView2.RestoreLayoutFromXml(My.Settings.ConfigPath & "\OpenMapGW02.xml")

            ConCM.Close()

            ' Me.BarStaticItem1.Caption = "JLD004 / Form5 : " & GridView1.RowCount.ToString


        Catch ex As OleDbException

        End Try
        GetData1 = ""

    End Function

    Function GetData2(ByVal selectCommand As String)
        GetData2 = ""

        Try
            Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
            Dim cmd As New System.Data.SqlClient.SqlCommand
            Dim reader As SqlClient.SqlDataReader = Nothing

            cmd.CommandText = selectCommand
            cmd.Connection = sqlConnection1

            sqlConnection1.Open()

            reader = cmd.ExecuteReader
            While reader.Read
                GetData2 = Trim(reader(0).ToString)
            End While
            sqlConnection1.Close()

        Catch ex As OleDbException

        End Try


    End Function


    Function GetDoor(ByVal Techniker As String)

        Try
            Dim ConCM As MySqlConnection
            Dim Reader As MySqlDataReader = Nothing
            Dim x As Decimal
            Dim y As Decimal
            Dim tech As String
            Dim i As Integer = 0
            Dim ii As Integer = 0
            Dim move As Boolean = False

            Dim Maintenance0 As System.Drawing.Image = My.Resources.Resources.red_icon_24_0
            Dim Maintenance1 As System.Drawing.Image = My.Resources.Resources.red_icon_24_1
            Dim Maintenance2 As System.Drawing.Image = My.Resources.Resources.red_icon_24_2
            Dim Maintenance3 As System.Drawing.Image = My.Resources.Resources.red_icon_24_3
            Dim Maintenance4 As System.Drawing.Image = My.Resources.Resources.red_icon_24_4
            Dim Maintenance5 As System.Drawing.Image = My.Resources.Resources.red_icon_24_5
            Dim Maintenance6 As System.Drawing.Image = My.Resources.Resources.red_icon_24_6
            Dim Maintenance7 As System.Drawing.Image = My.Resources.Resources.red_icon_24_7

            Dim Service0 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__0
            Dim Service1 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__1
            Dim Service2 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__2
            Dim Service3 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__3
            Dim Service4 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__4
            Dim Service5 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__5
            Dim Service6 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__6
            Dim Service7 As System.Drawing.Image = My.Resources.Resources.Sign_Error_icon_24__7


            Dim oDate As String = ""
            Dim status As String

            Dim DeltaPos As Boolean = False


            SV_200.Markers.Clear()
            SV_400.Markers.Clear()
            SV_500.Markers.Clear()
            SV_600.Markers.Clear()
            SV_610.Markers.Clear()
            SV_700.Markers.Clear()

            WM_200.Markers.Clear()
            WM_400.Markers.Clear()
            WM_500.Markers.Clear()
            WM_600.Markers.Clear()
            WM_610.Markers.Clear()
            WM_700.Markers.Clear()

            NO_200.Markers.Clear()
            NO_400.Markers.Clear()
            NO_500.Markers.Clear()
            NO_600.Markers.Clear()
            NO_610.Markers.Clear()
            NO_700.Markers.Clear()

            History.Markers.Clear()

            'ValDatKunAdrName7979
            ConCM = New MySqlConnection(My.Settings.MySQL)
            Dim cmd As New MySqlCommand("select Cast(ValDatObjGeoBreitengraddaa3 / 3600000 AS Decimal (18,13)), " & _
                                        "Cast( ValDatObjGeoLaengengrad7edc / 3600000 AS Decimal (18,13)), ValDatBodExterneauftragsnud106, from_unixtime(ValDatBodGeplanterabschlus2927/1000),  " & _
                                        "ValDatBodAuftragsartede4, ValDatObjAdrNamede48, ValAblStatusb22e, count(*), ValDatBodStoerungsbeschreicf2b, ValDatObjAdrTelefon26cb " & _
                                        "FROM auftragrow JOIN auftragrowvaldatobjtuerene261  ON auftragrow.Kee = auftragrowvaldatobjtuerene261.FkKee " & _
                                        " WHERE ValAblStatusb22e in ( '200','400','500','600','610','700') " & _
                                        "AND (ValAblTecMandanted4 = '" & My.Settings.CadisEnv & "' OR ValAblTecMandanted4 is NULL ) AND ValDatBodGeplanterabschlus2927 >= '1262453880000' " & _
                                        "AND (ValAblTecTechnikeridfc40 Like '" & Techniker & "' or ValAblTecTechnikeridfc40 is null)  " & _
                                        "Group BY Cast(ValDatObjGeoBreitengraddaa3 / 3600000 AS Decimal (18,13)),   " & _
                                        "Cast( ValDatObjGeoLaengengrad7edc / 3600000 AS Decimal (18,13)), from_unixtime(ValDatBodGeplanterabschlus2927/1000), ValDatBodAuftragsartede4, ValDatKunAdrName7979," & _
                                        "ValDatBodExterneauftragsnud106", ConCM)

            ConCM.Open()

            Reader = cmd.ExecuteReader
            While Reader.Read

                move = False

                x = Val(Reader(0).ToString)
                y = Val(Reader(1).ToString)
                tech = Trim(Reader(2).ToString)
                oDate = Reader(3).ToString
                status = Reader(4).ToString & "_" & Reader(6).ToString

                currentMarker = New CarMarker(New PointLatLng(x, y))
                currentMarker.ToolTipText = Reader(4).ToString & " " & Trim(Reader(2).ToString) & vbCrLf _
                    & " # of doors: " & Trim(Reader(7).ToString) & vbCrLf _
                    & oDate & vbCrLf & Trim(Reader(5).ToString) & vbCrLf & vbCrLf _
                    & Trim(Reader(8).ToString) & vbCrLf _
                    & "Tel: " & Trim(Reader(9).ToString)
                currentMarker.IsHitTestVisible = True

                Select Case status
                    Case Is = "SV_200"
                        currentMarker.Image = Service2
                        currentMarker.Tag = "SV_200"
                        SV_200.Markers.Add(currentMarker)
                    Case Is = "SV_400"
                        currentMarker.Image = Service4
                        currentMarker.Tag = "SV_400"
                        SV_400.Markers.Add(currentMarker)
                    Case Is = "SV_500"
                        currentMarker.Image = Service5
                        currentMarker.Tag = "SV_500"
                        SV_500.Markers.Add(currentMarker)
                    Case Is = "SV_600"
                        currentMarker.Image = Service6
                        currentMarker.Tag = "SV_600"
                        SV_600.Markers.Add(currentMarker)
                    Case Is = "SV_610"
                        currentMarker.Image = Service6
                        currentMarker.Tag = "SV_610"
                        SV_610.Markers.Add(currentMarker)
                    Case Is = "SV_700"
                        currentMarker.Image = Service7
                        currentMarker.Tag = "SV_700"
                        SV_700.Markers.Add(currentMarker)

                    Case Is = "WM_200"
                        currentMarker.Image = Maintenance2
                        currentMarker.Tag = "WM_200"
                        WM_200.Markers.Add(currentMarker)
                    Case Is = "WM_400"
                        currentMarker.Image = Maintenance4
                        currentMarker.Tag = "WM_400"
                        WM_400.Markers.Add(currentMarker)
                    Case Is = "WM_500"
                        currentMarker.Image = Maintenance5
                        currentMarker.Tag = "WM_500"
                        WM_500.Markers.Add(currentMarker)
                    Case Is = "WM_600"
                        currentMarker.Image = Maintenance6
                        currentMarker.Tag = "WM_600"
                        WM_600.Markers.Add(currentMarker)
                    Case Is = "WM_610"
                        currentMarker.Image = Maintenance6
                        currentMarker.Tag = "WM_610"
                        WM_610.Markers.Add(currentMarker)
                    Case Is = "WM_700"
                        currentMarker.Image = Maintenance7
                        currentMarker.Tag = "WM_700"
                        WM_700.Markers.Add(currentMarker)

                    Case Is = "NO_200"
                        currentMarker.Image = Service2
                        currentMarker.Tag = "NO_200"
                        NO_200.Markers.Add(currentMarker)
                    Case Is = "NO_400"
                        currentMarker.Image = Service4
                        currentMarker.Tag = "NO_400"
                        NO_400.Markers.Add(currentMarker)
                    Case Is = "NO_500"
                        currentMarker.Image = Service5
                        currentMarker.Tag = "NO_500"
                        NO_500.Markers.Add(currentMarker)
                    Case Is = "NO_600"
                        currentMarker.Image = Service6
                        currentMarker.Tag = "NO_600"
                        NO_600.Markers.Add(currentMarker)
                    Case Is = "NO_610"
                        currentMarker.Image = Service6
                        currentMarker.Tag = "NO_610"
                        NO_610.Markers.Add(currentMarker)
                    Case Is = "NO_700"
                        currentMarker.Image = Service7
                        currentMarker.Tag = "NO_700"
                        NO_700.Markers.Add(currentMarker)

                End Select

            End While

            ConCM.Close()

        Catch ex As OleDbException

        End Try
        GetDoor = ""

    End Function

    Private Sub BarButtonItem1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.BingMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.OpenStreetMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.GoogleSatellite
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
        BSRefresh.Caption = "Auto refresh in: " & Time & " s"

        If Not BCCheck.Checked Then

            If Time = 0 Then
                GetData()
                If ActivTech <> "" And My.Settings.TechTrace Then ShowRoute(ActivTech)
                If ActivTech <> "" Then GetDoor(ActivTech)
                Time = 30
            Else
                Time = Time - 1
            End If
        End If
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Me.GMapControl1.MapType = GMap.NET.MapType.GoogleMap
        Me.GMapControl1.ReloadMap()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick

        If Not BCCheck.Checked Then
            'Standars view
            GetDoor(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNO"))
            ActivTech = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNO")
            If ActivTech <> "" And My.Settings.TechTrace Then ShowRoute(ActivTech)
            Label1.Text = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNM")
        Else
            'Show daly log
            ActivTech = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNO")
            ShowLog(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNO"), BarEditItem1.EditValue)
            Label1.Text = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "EAEMNM")
        End If

    End Sub

    Private Sub GMapControl1_OnCurrentPositionChanged(point As GMap.NET.PointLatLng) Handles GMapControl1.OnCurrentPositionChanged
        screen.Markers(0).Position = GMapControl1.Position
    End Sub

    Private Sub GMapControl1_OnMapZoomChanged() Handles GMapControl1.OnMapZoomChanged
        Me.TrackBarControl1.Value = Me.GMapControl1.Zoom
        'screen.Markers(0).Position = GMapControl1.Position
        GMapControl1.Position = screen.Markers(0).Position

    End Sub

    Private Sub GMapControl1_OnMarkerClick(item As GMap.NET.WindowsForms.GMapMarker, e As System.Windows.Forms.MouseEventArgs) Handles GMapControl1.OnMarkerClick

        Dim s() As String

        If item.Tag = "AUTO" Then
            s = Split(item.ToolTipText, "/")
            Label1.Text = s(1)
            ActivTech = Trim(s(0))
            GetDoor(Trim(s(0)))
            ' will show only if member of JLD004_TechTrace group
            If My.Settings.TechTrace Then ShowRoute(Trim(s(0)))
        End If

        If (item.Tag = "WM_200" Or item.Tag = "WM_400" Or item.Tag = "WM_500" Or item.Tag = "SV_200" Or item.Tag = "SV_400" Or item.Tag = "SV_500") And My.Settings.Dispatch Then

            s = Split(item.ToolTipText, vbCrLf)
            s = Split(s(0), " ")
            If Not s(1).Substring(0, 1) = "f" Then
                Dispatch.Show()
                Dispatch.OrderNb.Text = Trim(s(1))
            Else
                MsgBox("can't be dispatched...  ")
            End If
        End If

    End Sub

    Private Sub BC_SV_200_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_200.CheckedChanged
        My.Settings.BC_SV_200 = BC_SV_200.Checked
        SetLayout()
    End Sub

    Private Sub BC_SV_400_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_400.CheckedChanged
        My.Settings.BC_SV_400 = BC_SV_400.Checked
        SetLayout()
    End Sub

    Private Sub BC_SV_500_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_500.CheckedChanged
        My.Settings.BC_SV_500 = BC_SV_500.Checked
        SetLayout()
    End Sub

    Private Sub BC_SV_600_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_600.CheckedChanged
        My.Settings.BC_SV_600 = BC_SV_600.Checked
        SetLayout()
    End Sub

    Private Sub BC_SV_610_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_610.CheckedChanged
        My.Settings.BC_SV_610 = BC_SV_610.Checked
        SetLayout()
    End Sub

    Private Sub BC_SV_700_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_SV_700.CheckedChanged
        My.Settings.BC_SV_700 = BC_SV_700.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_200_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_200.CheckedChanged
        My.Settings.BC_WM_200 = BC_WM_200.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_400_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_400.CheckedChanged
        My.Settings.BC_WM_400 = BC_WM_400.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_500_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_500.CheckedChanged
        My.Settings.BC_WM_500 = BC_WM_500.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_600_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_600.CheckedChanged
        My.Settings.BC_WM_600 = BC_WM_600.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_610_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_610.CheckedChanged
        My.Settings.BC_WM_610 = BC_WM_610.Checked
        SetLayout()
    End Sub

    Private Sub BC_WM_700_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_WM_700.CheckedChanged
        My.Settings.BC_WM_700 = BC_WM_700.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_200_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_200.CheckedChanged
        My.Settings.BC_NO_200 = BC_NO_200.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_400_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_400.CheckedChanged
        My.Settings.BC_NO_400 = BC_NO_400.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_500_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_500.CheckedChanged
        My.Settings.BC_NO_500 = BC_NO_500.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_600_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_600.CheckedChanged
        My.Settings.BC_NO_600 = BC_NO_600.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_610_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_610.CheckedChanged
        My.Settings.BC_NO_610 = BC_NO_610.Checked
        SetLayout()
    End Sub

    Private Sub BC_NO_700_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BC_NO_700.CheckedChanged
        My.Settings.BC_NO_700 = BC_NO_700.Checked
        SetLayout()
    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick

        Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
        openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        openFileDialog1.FilterIndex = 1
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "gpx files (*.gpx)|*.gpx"

        Dim lat As Double
        Dim lon As Double

        routes.Routes.Clear()

        If openFileDialog1.ShowDialog() = DialogResult.OK Then

            Dim gpx As String = File.ReadAllText(openFileDialog1.FileName)
            Dim r As gpxType = GMaps.Instance.DeserializeGPX(gpx)

            If r.trk.Length > 0 Then

                For Each trk As Object In r.trk

                    Dim points As New List(Of PointLatLng)

                    For Each seg As GMap.NET.trksegType In trk.trkseg

                        For Each p As GMap.NET.wptType In seg.trkpt
                            lat = p.lat
                            lon = p.lon
                            points.Add(New PointLatLng(lat, lon))
                        Next
                    Next

                    Dim rt As GMapRoute = New GMapRoute(points, String.Empty)

                    rt.Stroke = New Pen(Color.FromArgb(255, Color.Red))
                    rt.Stroke.Width = 5
                    rt.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid

                    routes.Routes.Add(rt)

                Next

                GMapControl1.ZoomAndCenterRoutes(Nothing)
            End If
        End If
    End Sub

    Function ShowRoute(ByVal TechID As String)

        routes.Routes.Clear()

        Dim points As New List(Of PointLatLng)

        Dim lat As Double
        Dim lon As Double

        Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
        Dim cmd As New System.Data.SqlClient.SqlCommand
        Dim reader As SqlClient.SqlDataReader = Nothing
        Dim dDate As Date = Date.Today

        cmd.Connection = sqlConnection1

        cmd.CommandText = "SELECT JLLAT, JLLONG FROM F4001  where JLTECHID = " & Trim(TechID) & " AND JLTDAY > '" & dDate.ToString("yyyy-MM-dd 00:00:00.000") & "' ORDER BY  ID desc"
        sqlConnection1.Open()

        reader = cmd.ExecuteReader()

        While reader.Read

            lat = reader(0).ToString
            lon = reader(1).ToString
            points.Add(New PointLatLng(lat, lon))

        End While

        Dim rt As GMapRoute = New GMapRoute(points, String.Empty)

        rt.Stroke = New Pen(Color.FromArgb(255, Color.Red))
        rt.Stroke.Width = 3
        rt.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid

        routes.Routes.Add(rt)
        'GMapControl1.ZoomAndCenterRoutes(Nothing)

        sqlConnection1.Close()


        ShowRoute = True


    End Function

    Function ShowAllRoute(ByVal dDate As Date)

        routes.Routes.Clear()

        Dim points As New List(Of PointLatLng)

        Dim lat As Double
        Dim lon As Double
        Dim firstrun As Boolean = True
        Dim Tech As String = ""
        Dim km As Double = 0

        Dim rt As GMapRoute

        Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
        Dim cmd As New System.Data.SqlClient.SqlCommand
        Dim reader As SqlClient.SqlDataReader = Nothing

        Dim RouteColor As New List(Of Color)
        Dim index As Integer = 0

        RouteColor.Add(Color.BlueViolet)
        RouteColor.Add(Color.CornflowerBlue)
        RouteColor.Add(Color.DarkMagenta)
        RouteColor.Add(Color.DarkBlue)
        RouteColor.Add(Color.DarkSalmon)
        RouteColor.Add(Color.DarkOrange)
        RouteColor.Add(Color.DarkTurquoise)
        RouteColor.Add(Color.DeepPink)
        RouteColor.Add(Color.DarkViolet)
        RouteColor.Add(Color.Blue)
        RouteColor.Add(Color.DarkOliveGreen)
        RouteColor.Add(Color.DarkMagenta)
        RouteColor.Add(Color.Chocolate)
        RouteColor.Add(Color.Cyan)
        RouteColor.Add(Color.Black)

        SV_200.Markers.Clear()
        SV_400.Markers.Clear()
        SV_500.Markers.Clear()
        SV_600.Markers.Clear()
        SV_610.Markers.Clear()
        SV_700.Markers.Clear()

        WM_200.Markers.Clear()
        WM_400.Markers.Clear()
        WM_500.Markers.Clear()
        WM_600.Markers.Clear()
        WM_610.Markers.Clear()
        WM_700.Markers.Clear()

        NO_200.Markers.Clear()
        NO_400.Markers.Clear()
        NO_500.Markers.Clear()
        NO_600.Markers.Clear()
        NO_610.Markers.Clear()
        NO_700.Markers.Clear()

        car_layer.Markers.Clear()

        History.Markers.Clear()


        cmd.Connection = sqlConnection1

        cmd.CommandText = "SELECT JLLAT, JLLONG, JLTECHID FROM F4001  where  JLTDAY > '" & dDate.ToString("yyyy-MM-dd 00:00:00.000") & "' AND JLTDAY < '" & dDate.ToString("yyyy-MM-dd 23:59:00.000") & "' ORDER BY  JLTECHID desc ,ID desc"
        sqlConnection1.Open()

        reader = cmd.ExecuteReader()

        While reader.Read
            If firstrun Then
                lat = reader(0).ToString
                lon = reader(1).ToString
                Tech = reader(2).ToString
                points.Add(New PointLatLng(lat, lon))
                firstrun = False

            Else
                lat = reader(0).ToString
                lon = reader(1).ToString
                If Tech = reader(2).ToString Then
                    points.Add(New PointLatLng(lat, lon))
                Else
                    rt = New GMapRoute(points, String.Empty)
                    rt.Stroke = New Pen(Color.FromArgb(255, RouteColor(index)))
                    If index = 14 Then index = 0 Else index = index + 1
                    rt.Stroke.Width = 3
                    rt.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid
                    routes.Routes.Add(rt)

                    'Calculate km
                    For a = 1 To points.Count - 1
                        km = km + Math.Sqrt((points(a).Lat - points(a - 1).Lat) ^ 2 + (points(a).Lng - points(a - 1).Lng) ^ 2)
                    Next

                    points.Clear()
                    points.Add(New PointLatLng(lat, lon))
                End If

                Tech = reader(2).ToString

            End If

        End While

        rt = New GMapRoute(points, String.Empty)

        rt.Stroke = New Pen(Color.FromArgb(255, RouteColor(index)))
        If index = 14 Then index = 0 Else index = index + 1
        rt.Stroke.Width = 3
        rt.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid

        routes.Routes.Add(rt)

        'Calculate km
        For a = 1 To points.Count - 1
            km = km + Math.Sqrt((points(a).Lat - points(a - 1).Lat) ^ 2 + (points(a).Lng - points(a - 1).Lng) ^ 2)
        Next
        km = km * 111
        Me.Label1.Text = "Total of [km]: " & Math.Round(km, 0)
        sqlConnection1.Close()


        ShowAllRoute = True


    End Function

    Function ShowLog(ByVal TechID As String, dDate As Date)

        routes.Routes.Clear()

        Dim points As New List(Of PointLatLng)

        Dim lat As Double
        Dim lon As Double
        Dim lDate As New List(Of Date)
        Dim Start As System.Drawing.Image = My.Resources.Resources.start_24
        Dim Stopp As System.Drawing.Image = My.Resources.Resources.stop_24
        Dim km As Double = 0


        SV_200.Markers.Clear()
        SV_400.Markers.Clear()
        SV_500.Markers.Clear()
        SV_600.Markers.Clear()
        SV_610.Markers.Clear()
        SV_700.Markers.Clear()

        WM_200.Markers.Clear()
        WM_400.Markers.Clear()
        WM_500.Markers.Clear()
        WM_600.Markers.Clear()
        WM_610.Markers.Clear()
        WM_700.Markers.Clear()

        NO_200.Markers.Clear()
        NO_400.Markers.Clear()
        NO_500.Markers.Clear()
        NO_600.Markers.Clear()
        NO_610.Markers.Clear()
        NO_700.Markers.Clear()

        car_layer.Markers.Clear()

        History.Markers.Clear()

        Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(My.Settings.CADISConnectionString)
        Dim cmd As New System.Data.SqlClient.SqlCommand
        Dim reader As SqlClient.SqlDataReader = Nothing

        cmd.Connection = sqlConnection1

        cmd.CommandText = "SELECT JLLAT, JLLONG, JLTDAY FROM F4001  where JLTECHID = " & Trim(TechID) & " AND JLTDAY > '" & dDate.ToString("yyyy-MM-dd 00:00:00.000") & "' AND JLTDAY < '" & dDate.ToString("yyyy-MM-dd 23:59:00.000") & "' ORDER BY  ID desc"
        sqlConnection1.Open()

        reader = cmd.ExecuteReader()

        While reader.Read

            lat = reader(0).ToString
            lon = reader(1).ToString
            points.Add(New PointLatLng(lat, lon))
            lDate.Add(reader(2).ToString)

        End While

        Dim rt As GMapRoute = New GMapRoute(points, String.Empty)

        rt.Stroke = New Pen(Color.FromArgb(255, Color.Red))
        rt.Stroke.Width = 3
        rt.Stroke.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid

        routes.Routes.Add(rt)

        sqlConnection1.Close()

        Dim ConCM As MySqlConnection
        Dim Reader1 As MySqlDataReader = Nothing
        Dim x As Decimal
        Dim y As Decimal
        Dim s As String
        Dim tech As String

        Dim img As New List(Of System.Drawing.Image)

        img.Add(My.Resources.Resources.red_icon_24_0)
        img.Add(My.Resources.Resources.red_icon_24_1)
        img.Add(My.Resources.Resources.red_icon_24_2)
        img.Add(My.Resources.Resources.red_icon_24_3)
        img.Add(My.Resources.Resources.red_icon_24_4)
        img.Add(My.Resources.Resources.red_icon_24_5)
        img.Add(My.Resources.Resources.red_icon_24_6)
        img.Add(My.Resources.Resources.red_icon_24_7)
        img.Add(My.Resources.Resources.red_icon_24_8)
        img.Add(My.Resources.Resources.red_icon_24_9)
        img.Add(My.Resources.Resources.red_icon_24_A)
        img.Add(My.Resources.Resources.red_icon_24_B)
        img.Add(My.Resources.Resources.red_icon_24_C)
        img.Add(My.Resources.Resources.red_icon_24_D)
        img.Add(My.Resources.Resources.red_icon_24_F)

        Dim oDate As String = ""
        Dim status As String
        Dim sList As New List(Of String)

        Dim DeltaPos As Boolean = False

        ConCM = New MySqlConnection(My.Settings.MySQL)
        Dim cmd1 As New MySqlCommand("select Cast(ValDatObjGeoBreitengraddaa3 / 3600000 AS Decimal (18,13)), " & _
                                    "Cast( ValDatObjGeoLaengengrad7edc / 3600000 AS Decimal (18,13)), ValDatBodExterneauftragsnud106, from_unixtime(ValDatBodGeplanterabschlus2927/1000),  " & _
                                    "ValDatBodAuftragsartede4, ValDatKunAdrName7979, ValAblStatusb22e, count(*), ValDatBodStoerungsbeschreicf2b, ValDatObjAdrTelefon26cb " & _
                                    "FROM auftragrow JOIN auftragrowvaldatobjtuerene261  ON auftragrow.Kee = auftragrowvaldatobjtuerene261.FkKee " & _
                                    "WHERE ValAblStatusb22e in ('900') " & _
                                    "AND from_unixtime(ValAblRueEinsatzende27f/1000) > '" & dDate.ToString("yyyy-MM-dd 00:00:00.000") & "'" & _
                                    "AND from_unixtime(ValAblRueEinsatzende27f/1000) < '" & dDate.ToString("yyyy-MM-dd 23:59:00.000") & "'" & _
                                    "AND ValAblTecTechnikeridfc40 = '" & TechID & "'  " & _
                                    "Group BY Cast(ValDatObjGeoBreitengraddaa3 / 3600000 AS Decimal (18,13)),   " & _
                                    "Cast( ValDatObjGeoLaengengrad7edc / 3600000 AS Decimal (18,13)), from_unixtime(ValDatBodGeplanterabschlus2927/1000), ValDatBodAuftragsartede4, ValDatKunAdrName7979," & _
                                    "ValDatBodExterneauftragsnud106  order by ValAblRueEinsatzende27f", ConCM)

        ConCM.Open()

        Dim z As Integer = 0

        Reader1 = cmd1.ExecuteReader
        While Reader1.Read

            x = Val(Reader1(0).ToString)
            y = Val(Reader1(1).ToString)
            tech = Trim(Reader1(2).ToString)
            oDate = Reader1(3).ToString
            status = Reader1(4).ToString & "_" & Reader1(6).ToString

            currentMarker = New CarMarker(New PointLatLng(x, y))
            currentMarker.ToolTipText = Reader1(4).ToString & " " & Trim(Reader1(2).ToString) & vbCrLf & " # of doors: " & Trim(Reader1(7).ToString) & vbCrLf & oDate & vbCrLf & Trim(Reader1(5).ToString) & vbCrLf & vbCrLf & Trim(Reader1(8).ToString) & vbCrLf & "Tel: " & Trim(Reader1(9).ToString)
            currentMarker.IsHitTestVisible = True

            sList.Add(Reader1(4).ToString & " " & Trim(Reader1(2).ToString) & " / # of doors: " & Trim(Reader1(7).ToString) & " / " & oDate & " / " & Trim(Reader1(5).ToString))

            currentMarker.Image = img(z)
            currentMarker.Tag = "History"
            History.Markers.Add(currentMarker)

            z = z + 1

        End While

        ConCM.Close()

        'Start and stop route
        If points.Count > 0 Then
            lat = points(points.Count - 1).Lat
            lon = points(points.Count - 1).Lng

            currentMarker = New CarMarker(New PointLatLng(lat, lon))
            currentMarker.Image = Start
            currentMarker.ToolTipText = "Start time: " & lDate(lDate.Count - 1).ToString
            currentMarker.IsHitTestVisible = True
            currentMarker.Tag = "History"
            History.Markers.Add(currentMarker)

            'Calculate km

            For a = 1 To points.Count - 1
                km = km + Math.Sqrt((points(a).Lat - points(a - 1).Lat) ^ 2 + (points(a).Lng - points(a - 1).Lng) ^ 2)
            Next

            km = km * 111

            lat = points(0).Lat
            lon = points(0).Lng

            currentMarker = New CarMarker(New PointLatLng(lat, lon))
            currentMarker.Image = Stopp

            s = "Stop time: " & lDate(0).ToString & vbCrLf & "Distance: " & Math.Round(km, 0) & " [km]"

            For a = 0 To sList.Count - 1
                s = s & vbCrLf & sList(a)
            Next

            currentMarker.ToolTipText = s
            currentMarker.IsHitTestVisible = True
            currentMarker.Tag = "History"
            History.Markers.Add(currentMarker)

        End If

        ShowLog = True

    End Function

    Private Sub BCCheck_CheckedChanged(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BCCheck.CheckedChanged

        If BCCheck.Checked = False Then
            Time = 0
        Else
            If ActivTech <> "" Then ShowLog(ActivTech, BarEditItem1.EditValue)
        End If

    End Sub

    Private Sub ShowActivJobsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ShowActivJobsToolStripMenuItem.Click


        ActivJob.Close()

        ActivJob.Show()

    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick

        GeoAddress.Show()

    End Sub

    Private Sub BarButtonItem7_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Form5.Show()

    End Sub

    Private Sub TrackBarControl1_ValueChanged(sender As Object, e As System.EventArgs) Handles TrackBarControl1.ValueChanged
        Me.GMapControl1.Zoom = Val(Me.TrackBarControl1.Value.ToString)
    End Sub

    Private Sub BarButtonItem8_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem8.ItemClick
        ShowAllRoute(BarEditItem1.EditValue)
        Time = 30
    End Sub

    Private Sub BarButtonItem10_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem10.ItemClick

        Me.Cursor() = Cursors.WaitCursor

        GetData1("SELECT from_unixtime(ValDatBodGeplanterabschlus2927/1000) AS Abschluss, ValDatBodExterneauftragsnud106 as Auftragsnummer, " & _
         "ValDatBodEinsatznummerccf5 as Einsatznummer, ValAblStatusb22e as Status, ValAblTecMandanted4 as Land, ValAblTecName60ff as Techniker, " & _
         "ValAblTecTechnikeridfc40 as 'Techniker ID', ValDatBodAuftragsartede4 as Auftragstyp , ValDatKunAdrName7979 as Kunde, " & _
         "ValDatObjAdrAdresszeile1787a as Adr1, ValDatObjAdrAdresszeile27879 as Adr2, ValDatObjAdrAdresszeile37878 as Adr3, ValDatObjAdrAdresszeile4787f as Adr4, " & _
         "ValDatObjAdrPlz7bf7 as PLZ, ValDatObjAdrOrt8627 as Ort, ValDatObjAdrEmail1acf as Email, " & _
         "(ValDatObjGeoBreitengraddaa3 / 3600000) as Lat, (ValDatObjGeoLaengengrad7edc / 3600000) as lng " & _
         "FROM auftragrow " & _
         "WHERE ValDatBodExterneauftragsnud106 Like '%" & Trim(TBOrder.Text.ToString) & "%' AND ValDatBodMandante3bc = '" & My.Settings.CadisEnv & "' " & _
         "ORDER BY ValDatBodExterneauftragsnud106  asc " & _
         "LIMIT 500")

        Me.Cursor() = Cursors.Default
    End Sub

    Private Sub ShowOnTheMapToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ShowOnTheMapToolStripMenuItem.Click

        Dim lng As Double = 0
        Dim lat As Double = 0

        Try
            lng = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "lng")
            lat = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Lat")
        Catch ex As Exception

        End Try


        Me.GMapControl1.Position = New PointLatLng(lat, lng)


        If Not BCCheck.Checked Then
            'Standars view
            GetDoor(GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker ID"))
            ActivTech = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker ID")
            If ActivTech <> "" And My.Settings.TechTrace Then ShowRoute(ActivTech)
            Label1.Text = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker")
        Else
            'Show daly log
            ActivTech = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker ID")
            ShowLog(GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker ID"), BarEditItem1.EditValue)
            Label1.Text = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Techniker")
        End If



    End Sub

    Private Sub SplitContainerControl1_SplitterPositionChanged(sender As Object, e As System.EventArgs) Handles SplitContainerControl1.SplitterPositionChanged
        My.Settings.Split2 = Me.SplitContainerControl1.SplitterPosition
    End Sub


    Private Sub GridView2_RowCellStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs) Handles GridView2.RowCellStyle
        Dim view As GridView = sender
        If e.Column.FieldName = "Status" Then
            Dim myOtherColumnValue As String = view.GetRowCellValue(e.RowHandle, "Status")
            If myOtherColumnValue = "200" Then e.Appearance.BackColor = Color.LightSalmon
            If myOtherColumnValue = "400" Then e.Appearance.BackColor = Color.LightYellow
            If myOtherColumnValue = "500" Then e.Appearance.BackColor = Color.LightGray
            If myOtherColumnValue = "600" Then e.Appearance.BackColor = Color.LightBlue
            If myOtherColumnValue = "610" Then e.Appearance.BackColor = Color.LightPink
            If myOtherColumnValue = "700" Then e.Appearance.BackColor = Color.Violet
            If myOtherColumnValue = "710" Then e.Appearance.BackColor = Color.Red
            If myOtherColumnValue = "900" Then e.Appearance.BackColor = Color.LightGreen

            If myOtherColumnValue = "999" Then e.Appearance.BackColor = Color.Black
            If myOtherColumnValue = "999" Then e.Appearance.ForeColor = Color.Yellow
        End If

    End Sub

    Public Shared Function GetTimestamp(ByVal FromDateTime As DateTime) As Integer
        Dim Startdate As DateTime = #1/1/1970#
        Dim Spanne As TimeSpan

        Spanne = FromDateTime.Subtract(Startdate)
        Return CType(Math.Abs(Spanne.TotalSeconds()), Integer)
    End Function


    Private Sub BarButtonItem11_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem11.ItemClick


        Dim tDate As Date = BarEditItem3.EditValue
        tDate = tDate.AddDays(1)
        Dim dStart As Integer = GetTimestamp(BarEditItem2.EditValue)
        Dim dStop As Integer = GetTimestamp(tDate)


        Me.Cursor() = Cursors.WaitCursor

        GetData1("SELECT from_unixtime(ValDatBodGeplanterabschlus2927/1000) AS Abschluss, ValDatBodExterneauftragsnud106 as Auftragsnummer, " & _
         "ValDatBodEinsatznummerccf5 as Einsatznummer, ValAblStatusb22e as Status, ValAblTecMandanted4 as Land, ValAblTecName60ff as Techniker, " & _
         "ValAblTecTechnikeridfc40 as 'Techniker ID', ValDatBodAuftragsartede4 as Auftragstyp , ValDatKunAdrName7979 as Kunde, " & _
         "ValDatObjAdrAdresszeile1787a as Adr1, ValDatObjAdrAdresszeile27879 as Adr2, ValDatObjAdrAdresszeile37878 as Adr3, ValDatObjAdrAdresszeile4787f as Adr4, " & _
         "ValDatObjAdrPlz7bf7 as PLZ, ValDatObjAdrOrt8627 as Ort, ValDatObjAdrEmail1acf as Email,  ValTueExtauftragsnummereb7b as Ext, " & _
         "(ValDatObjGeoBreitengraddaa3 / 3600000) as Lat, (ValDatObjGeoLaengengrad7edc / 3600000) as lng " & _
         "FROM (auftragrow auftragrow INNER JOIN auftragrowvaldatobjtuerene261 auftragrowvaldatobjtuerene261 ON auftragrow.Kee = auftragrowvaldatobjtuerene261.FkKee) " & _
         "WHERE ValDatBodMandante3bc = '" & My.Settings.CadisEnv & "' AND ValDatBodGeplanterabschlus2927/1000 >= " & dStart & " AND ValDatBodGeplanterabschlus2927/1000 <= " & dStop & " " & _
         "ORDER BY ValDatBodExterneauftragsnud106  asc " & _
         "LIMIT 500")

        Me.Cursor() = Cursors.Default


    End Sub

    Private Sub DispatchToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DispatchToolStripMenuItem.Click

        Dim i As String = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Status")
        Dim s As String = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Auftragsnummer")

        If (i = "200" Or i = "400" Or i = "500") And My.Settings.Dispatch Then

            If Not s.Substring(0, 1) = "f" Then
                Dispatch.Show()
                Dispatch.OrderNb.Text = Trim(s)
            Else
                MsgBox("can't be dispatched...  ")
            End If
        End If
    End Sub

    Private Sub ContextMenuStrip2_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip2.Opening

        Dim i As String = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Status")
        If (i = "200" Or i = "400" Or i = "500") And My.Settings.Dispatch Then
            ContextMenuStrip2.Items(1).Visible = True
        Else
            ContextMenuStrip2.Items(1).Visible = False
        End If

        'schow PDF menu
        If (i = "710" Or i = "900" Or i = "999") Then
            ContextMenuStrip2.Items(2).Visible = True
        Else
            ContextMenuStrip2.Items(2).Visible = False
        End If
     
    End Sub

    Private Sub OpenPDFToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenPDFToolStripMenuItem.Click

        Dim s As String
        Dim s1 As String = ""
        Dim Mask As String = ""
        Dim sPDFfile As String = ""
        Dim JLAUF As String = Trim(GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Auftragsnummer"))

        s = Form1.GetPDFData("SELECT JLPDF FROM F5001 WHERE JLEXTNUM = '" & JLAUF & "' OR JLAUF = '" & JLAUF & "'")

        If s <> "" Then
            'Open PDF new methode
            Mask = My.Settings.PDFPath & "\" & s
            sPDFfile = Dir(Mask, vbNormal)
            If sPDFfile <> "" Then Process.Start(My.Settings.PDFPath & "\" & sPDFfile)

        Else
            'Open PDF old methode
            Try
                s = GetData2("select top 1 m3ordernumber from dbo.M3Order  where idm in (select ID from auf where auftragsnummer = '" & JLAUF & "')")

                s = s.Substring(2, s.Length - 2)

                Mask = My.Settings.PDFPath & "\*" & s & "*.PDF"
                sPDFfile = Dir(Mask, vbNormal)
                If sPDFfile <> "" Then Process.Start(My.Settings.PDFPath & "\" & sPDFfile)

            Catch ex As Exception

            End Try
        End If


    End Sub
End Class